package com.feicheng.iou.controller;


import com.alibaba.fastjson.JSONObject;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.PaymentItem;
import com.feicheng.iou.entity.PaymentOptions;
import com.feicheng.iou.entity.ProjectService;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.BaomingPOJO;
import com.feicheng.iou.pojo.PaymentItemPOJO;
import com.feicheng.iou.pojo.PaymentPOJO;
import com.feicheng.iou.pojo.ResourcePOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.PaymentItemService;
import com.feicheng.iou.service.PaymentOptionsService;
import com.feicheng.iou.service.ProjectServiceService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/paymentItem")
@Slf4j
@Setter
@CrossOrigin
public class PaymentItemController {

    @Autowired
    PaymentOptionsService paymentOptionsService;
    @Autowired
    ProjectServiceService projectServiceService;
    @Autowired
    PaymentItemService paymentItemService;

    /**
     * @param paymentOptionsId,amount
     * @return
     * @Author luoxin
     * @Description 计算分期列表的接口
     * @Date 1:42 下午 2021/8/18
     */
    @GetMapping("/calculate")
    public ResultInfoV110 calculatePaymentOptionList(Integer paymentOptionsId, BigDecimal amount) {  //查询线索列表
        log.info("selectPaymentOptionList方法进入");
        PaymentOptions paymentOptions = paymentOptionsService.selectPaymentOptionsByid(paymentOptionsId);
        //调用ResourceService实现线索列表查询业务逻辑
        return paymentItemService.calculatePaymentItemList(paymentOptions, amount);
    }

    /**
     * @param paymentId
     * @return
     * @Author luoxin
     * @Description 查询分期数据列表
     * @Date 3:02 下午 2021/8/18
     */
    @GetMapping("/list/{paymentId}")
    public ResultInfoV110 selectPaymentItemListByPid(@PathVariable("paymentId") Integer paymentId) {  //查询线索列表
        log.info("selectPaymentItemList方法进入");
        //调用ResourceService实现线索列表查询业务逻辑
        return paymentItemService.selectPaymentItemListByPid(paymentId);
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description 收入查询接口
     * @Date 2:43 下午 2021/8/24
     */
    @GetMapping("/list")
    public ResultInfoV110 selectPaymentItemListByPid(String paymentItem) {  //查询线索列表

        log.info("selectPaymentItemList方法进入,参数为" + paymentItem);
        JSONObject jsonObject = JSONObject.parseObject(paymentItem);

        PaymentItemPOJO paymentItemPOJO = JSONObject.toJavaObject(jsonObject, PaymentItemPOJO.class);

        //调用ResourceService实现线索列表查询业务逻辑

        return paymentItemService.selectPaymentItemList(paymentItemPOJO);
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description 审核已还款接口
     * @Date 2:43 下午 2021/8/24
     */
    @PutMapping("/confirmed/{id}")
    public ResultInfoV110 confirmPayment(@PathVariable("id") Integer id, HttpServletRequest request) {
        log.info("confirmPayment方法进入，参数为: " + id);


        // 参数校验是否为空
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "审核"));
        }

        //调用paymentItemService实现线索添加业务逻辑
        return paymentItemService.confirmPayment(id, request);
    }

    /**
     * @Author luoxin
     * @Description 查询逾期未付数量接口
     * @Date 10:35 上午 2021/8/30
     * @param null
     * @return
     */
    @GetMapping("/list/unpaid/count")
    public ResultInfoV110 countUnpaid() {  //查询线索列表
        log.info("selectPaymentItemList方法进入");

        //调用paymentItemService实现线索列表查询业务逻辑
        return paymentItemService.countUnpaid();
    }

    /**
     * @Author luoxin
     * @Description 查询逾期未付数量列表接口
     * @Date 10:35 上午 2021/8/30
     * @param null
     * @return
     */
    @GetMapping("/list/unpaid")
    public ResultInfoV110 selectUnpaid() {  //查询线索列表
        log.info("selectPaymentItemList方法进入");

        System.out.println(paymentItemService.checkOverdue());
        //调用paymentItemService实现线索列表查询业务逻辑
        return paymentItemService.selectUnpaid();
    }

    /**
     * @Author luoxin
     * @Description 查询已还款待确认数量接口
     * @Date 10:35 上午 2021/8/30
     * @param null
     * @return
     */
    @GetMapping("/list/reviewed/count")
    public ResultInfoV110 countReviewed() {  //查询线索列表
        log.info("selectPaymentItemList方法进入");

        //调用paymentItemService实现线索列表查询业务逻辑
        return paymentItemService.countReviewed();
    }

    /**
     * @Author luoxin
     * @Description 查询已还款待确认列表接口
     * @Date 10:36 上午 2021/8/30
     * @param null
     * @return
     */
    @GetMapping("/list/reviewed")
    public ResultInfoV110 selectReviewed() {  //查询线索列表
        log.info("selectPaymentItemList方法进入");

        System.out.println(paymentItemService.checkOverdue());
        //调用paymentItemService实现线索列表查询业务逻辑
        return paymentItemService.selectReviewed();
    }

    /**
     * @Author luoxin
     * @Description 手动还款接口
     * @Date 4:31 下午 2021/8/30
     * @param null
     * @return
     */
    @PutMapping("/repayment")
    public ResultInfoV110 repayment(Integer id,String payMethod, HttpServletRequest request) {
        log.info("confirmPayment方法进入，参数为: " + id);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "审核"));
        }

        //调用paymentItemService实现线索添加业务逻辑
        return paymentItemService.repayment(id,payMethod, request);
    }

}
